package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.BasicOperatorEntity;
import com.iwomy.envpro.platform.entity.BasicParkEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.BasicParkService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础信息-所属园区
 *
 * @author pig
 * @date 2023-09-23 15:48:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicPark" )
@Tag(description = "basicPark" , name = "基础信息-所属园区管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicParkController {

    private final  BasicParkService basicParkService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicPark 基础信息-所属园区
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicPark_view')" )
    public R getBasicParkPage(@ParameterObject Page page,@ParameterObject BasicParkEntity basicPark) {
        LambdaQueryWrapper<BasicParkEntity> wrapper = Wrappers.lambdaQuery(basicPark);
        return R.ok(basicParkService.page(page, wrapper));
    }


    /**
     * 通过id查询基础信息-所属园区
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicPark_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicParkService.getById(id));
    }

    /**
     * 新增基础信息-所属园区
     * @param basicPark 基础信息-所属园区
     * @return R
     */
    @Operation(summary = "新增基础信息-所属园区" , description = "新增基础信息-所属园区" )
    @SysLog("新增基础信息-所属园区" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicPark_add')" )
    public R save(@RequestBody BasicParkEntity basicPark) {
		BasicParkEntity wrapperEntity = new BasicParkEntity();
		wrapperEntity.setPark(basicPark.getPark());
		LambdaQueryWrapper<BasicParkEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicParkEntity one = basicParkService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)){
			throw new PlatformException("所属园区已存在");
		}
        return R.ok(basicParkService.save(basicPark));
    }

    /**
     * 修改基础信息-所属园区
     * @param basicPark 基础信息-所属园区
     * @return R
     */
    @Operation(summary = "修改基础信息-所属园区" , description = "修改基础信息-所属园区" )
    @SysLog("修改基础信息-所属园区" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicPark_edit')" )
    public R updateById(@RequestBody BasicParkEntity basicPark) {
		BasicParkEntity wrapperEntity = new BasicParkEntity();
		wrapperEntity.setPark(basicPark.getPark());
		LambdaQueryWrapper<BasicParkEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicParkEntity one = basicParkService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)&&!one.getId().equals(basicPark.getId())){
			throw new PlatformException("所属园区已存在");
		}
        return R.ok(basicParkService.updateById(basicPark));
    }

    /**
     * 通过id删除基础信息-所属园区
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除基础信息-所属园区" , description = "通过id删除基础信息-所属园区" )
    @SysLog("通过id删除基础信息-所属园区" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicPark_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicParkService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicPark 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicPark_export')" )
    public List<BasicParkEntity> export(BasicParkEntity basicPark) {
        return basicParkService.list(Wrappers.query(basicPark));
    }
}